package de.tu_darmstadt.gdi1.xcom.model;

import java.util.LinkedList;
import java.util.List;

import de.tu_darmstadt.gdi1.framework.interfaces.IStepManager;
import de.tu_darmstadt.gdi1.framework.utils.Point;
import de.tu_darmstadt.gdi1.xcom.controller.ai.XcomAIInterface;
import de.tu_darmstadt.gdi1.xcom.model.elements.GameElement;
import de.tu_darmstadt.gdi1.xcom.model.elements.Mob;

public class XcomTeam {

	private List<Point> mobPositions;
	private int selectedMob;
	private Mob currentMob;
	
	private boolean useAI;
	private XcomAIInterface ai;
	
	public void addMob(Mob mob){
			if(!mobPositions.contains(mob.getPosition()))
				mobPositions.add(mob.getPosition());
	}
	
	public List<Point> getMobPositions() {
		return mobPositions;
	}
	
	public void setCurrentMobPosition(Point newPosition) {
		mobPositions.set(selectedMob, newPosition);
		currentMob.setPosition(newPosition);
	}
	
	public boolean deleteMob(Mob mob) {
		int index = mobPositions.indexOf(mob.getPosition());
		if(index < 0)
			return false;
		
		mobPositions.remove(mob.getPosition());
		
		if(index == selectedMob)
			return true;
		if(index < selectedMob){
			selectedMob--;
		}
		return false;
	}

	public void setSelectedMob(Mob selectedMob) {
			setSelectedMob(mobPositions.indexOf(selectedMob.getPosition()), selectedMob);
	}
	
	public void setSelectedMob(int selectedMob, Mob currentMob) {
		if(selectedMob < 0)
			return;
		this.selectedMob = selectedMob;
		this.currentMob = currentMob;
	}
	
	public void setMobs(List<Mob> mobs){
			mobPositions = new LinkedList<Point>();
		for(Mob currentMob : mobs) {
			mobPositions.add(currentMob.getPosition());
		}
	}
	
	public Mob getCurrentMob() {
		return currentMob;
	}
	
	public int getCurrentMobIndex(){
		return selectedMob;
	}
	
	public boolean isDefeated(){
		return (mobPositions.size() < 1);
	}
	
	
	public void resetTeam(IStepManager<GameElement> board){
		
		if(mobPositions.size() == 0) return;
		
		for(Point p : mobPositions){
			List<GameElement> elem = board.getElements(p);
			if(elem.get(elem.size() - 1) instanceof Mob)
				((Mob)elem.get(elem.size() - 1)).resetPoints();
		}
		selectedMob = 0;
		List<GameElement> field = board.getElements(mobPositions.get(0));
		if(field.get(field.size() - 1) instanceof Mob)
			currentMob = (Mob)field.get(field.size() - 1);
		else throw new IllegalArgumentException("an der angegebenen Position befindet sich kein Mob!");
	}
	
	public void setAI(XcomAIInterface ai){
		if(ai != null){
			this.ai = ai;
			useAI = true;
		}
	}
	
	public int getMOBNumber(){
		return mobPositions.size();
	}

	public XcomAIInterface getAi() {
		return ai;
	}

	public boolean isUseAI() {
		return useAI;
	}

	public void performReaction(Mob enemyMob) {
		List<Mob> canAttackList = getAttackList(enemyMob);
	}

	private List<Mob> getAttackList(Mob enemyMob) {
		List<Mob> result = new LinkedList<Mob>();
		
		return result;
	}
}
